﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Krile.Forms.MainForm.Controls.XTab
{
    public class XTabMover
    {
        XTab parent;
        public XTabMover(XTab par)
        {
            parent = par;
            //mouse event set
            parent.MouseDown += new MouseEventHandler(parent_MouseDown);
            parent.MouseMove += new MouseEventHandler(parent_MouseMove);
            parent.MouseUp += new MouseEventHandler(parent_MouseUp);
            parent.MouseLeave += new EventHandler(parent_MouseLeave);
        }

        void parent_MouseLeave(object sender, EventArgs e)
        {
            curItem = null;
        }

        void parent_MouseUp(object sender, MouseEventArgs e)
        {
            curItem = null;
        }

        private XTabItemBase curItem = null;

        void parent_MouseDown(object sender, MouseEventArgs e)
        {
            if (parent.SelectedItem != null && parent.SelectedItem != curItem && e.Button == MouseButtons.Left)
            {
                Rectangle cRect = parent.SelectedItem.ClientRectangle;
                if (e.Location.X >= cRect.Left && e.Location.X <= cRect.Right &&
                    e.Location.Y >= cRect.Top && e.Location.Y <= cRect.Bottom)
                {
                    curItem = parent.SelectedItem;
                }
            }
        }

        void parent_MouseMove(object sender, MouseEventArgs e)
        {
            if (parent.SelectedItem != null && curItem == parent.SelectedItem && e.Button == MouseButtons.Left)
            {
                parent.SetCurrentTabIndex(GetNearestMovableIndex(e.Location));
                parent.Refresh();
            }
        }

        #region PointControl
        private int GetNearestMovableIndex(Point curLoc)
        {
            Point[] array = GetMovablePoints();
            Array.Reverse(array);
            int curIndex = array.Length - 1;
            foreach (var point in array)
            {
                curIndex--;
                if (curIndex < 0) break;
                if (point.X < 0 || point.Y < 0) continue;
                if (point.Y > curLoc.Y)
                    continue;
                if (point.X > curLoc.X)
                    continue;
                return curIndex;
            }
            return 0;
        }

        private Point[] GetMovablePoints()
        {
            List<Point> ret = new List<Point>();
            ret.Add(new Point(XTab.outerMargin, XTab.outerMargin));
            foreach (var val in MovablePoints())
                ret.Add(val);
            return ret.ToArray();
        }

        private IEnumerable<Point> MovablePoints()
        {
            var items = parent.GetItemBaseArray();
            foreach (var item in items)
            {
                if (item is XTabItem)
                    yield return new Point(item.ClientRectangle.Left, item.ClientRectangle.Top);
            }
        }
        #endregion
    }
}
